﻿using System;
using System.Net;
using Microsoft.Phone.Net.NetworkInformation;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace CloudTaobao.Util
{
    public class Utils
    {
        public static string filterSpanString(string origin)
        {
            string filtered = origin;
            if (filtered.Contains("span"))
            {
                filtered = filtered.Replace("<span class=H>", "[");
                filtered = filtered.Replace("</span>", "]");
            }
            return filtered;
        }

        public static bool isShowPic()
        {
            App app = Application.Current as App;
            if (NetworkInterfaceType.Wireless80211 != app.netType
                && !app.isShowPicNoWIFI)
                return false;
            else
                return true;
        }

        public static int getRecordsNum()
        {
            App app = Application.Current as App;
            if (NetworkInterfaceType.Wireless80211 != app.netType)
                return app.recordsNoWIFI;
            else
                return app.records;
        }

        public static string removeEmptyLines(string origin)
        {
            string tmp = origin;
            while (tmp.Contains("\n\n"))
            {
                tmp = tmp.Replace("\n\n", "\n");
            }
            return tmp;
        }

        public static void Quit()
        {
            throw new QuitException();
        }

        public static DoubleAnimation getTransparentAnimation(double from, double to, double seconds, DependencyObject targetControl)
        {
            // 创建和定义 animation
            DoubleAnimation anima = new DoubleAnimation();
            anima.From = from;//开始的值
            anima.To = to;//结束的值
            anima.Duration = new Duration(TimeSpan.FromSeconds(seconds));//持续的时间

            Storyboard.SetTarget(anima, targetControl);
            Storyboard.SetTargetProperty(anima, new PropertyPath(UIElement.OpacityProperty));
            return anima;
        }

        public static Timeline getMarginChangeAnimation(Thickness from, Thickness to, Duration ticks, DependencyObject targetControl)
        {
            return ThicknessAnimation.Create(targetControl, FrameworkElement.MarginProperty, ticks, from, to);
        }
    }
}
